package com.chen.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chen.entity.User;
import com.chen.response.Result;
import com.chen.service.OssService;
import com.chen.service.UserService;
import com.chen.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author chen
 * @since 2022-12-13
 */
@Api(tags = "用户模块")
//@CrossOrigin
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private OssService ossService;

    @ApiOperation(value = "获取所以用户集合", notes = "详细描述这个接口是干嘛用的")
    @GetMapping("/getUserList")
    public List<User> getUserList() {
        List<User> userList = userService.list();
        return userList;
    }

    @ApiOperation(value = "获取用户列表", notes = "详细描述这个接口是干嘛用的")
    @PostMapping("/getUsers")
    public Result getUsers(@RequestParam(value = "current", required = true, defaultValue = "1") Integer current,
                           @RequestParam(value = "size", required = true, defaultValue = "7") Integer size,
                           @RequestBody UserVO userVO) {
        Page<User> page = new Page<>(current, size);
        Page<User> userPage = userService.selectUserByPage(page, userVO);
        return Result.ok().data("userPage", userPage);
    }

    @ApiOperation(value = "创建bucket")
    @PostMapping("/createBucket")
    public Result createBucket() {
        ossService.createBucket();
        return Result.ok();
    }

    @ApiOperation(value = "导出用户信息")
    @PostMapping(value = "/export")
    public void exportUserExcel(HttpServletResponse response, @RequestBody UserVO userVO) throws IOException {
        List<User> userList = userService.selectUserList(userVO);
        userService.exportUserExcel(response,userList);
    }
}
